home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / think / AmigaGnuChess.lha / chess / src.lha / src / init.c < prev    next >
C/C++ Source or Header  |  1992-09-06  |  12KB  |  486 lines

  1. /*
  2.  * init.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #include "gnuchess.h"
  24.  
  25. /* .... MOVE GENERATION VARIABLES AND INITIALIZATIONS .... */
  26.  
  27.  
  28. short distdata[64][64], taxidata[64][64];
  29.  
  30. #ifdef KILLT
  31. /* put moves to the center first */
  32. void
  33. Initialize_killt (void)
  34. {
  35.   register unsigned short f, t, s;
  36.   register short d;
  37.   for (f = 64; f--;)
  38.     for (t = 64; t--;)
  39.       {
  40.     d = taxidata[f][0x1b];
  41.     if (taxidata[f][0x1c] < d)
  42.       d = taxidata[f][0x1c];
  43.     if (taxidata[f][0x23] < d)
  44.       d = taxidata[f][0x23];
  45.     if (taxidata[f][0x24] < d)
  46.       d = taxidata[f][0x24];
  47.     s = d;
  48.     d = taxidata[t][0x1b];
  49.     if (taxidata[t][0x1c] < d)
  50.       d = taxidata[t][0x1c];
  51.     if (taxidata[t][0x23] < d)
  52.       d = taxidata[t][0x23];
  53.     if (taxidata[t][0x24] < d)
  54.       d = taxidata[t][0x24];
  55.     s -= d;
  56.     killt[(f << 8) | t] = s;
  57.     killt[(f << 8) | t | 0x80] = s;
  58.       }
  59. }
  60. #endif
  61. void
  62. Initialize_dist (void)
  63. {
  64.   register short a, b, d, di;
  65.  
  66.   for (a = 0; a < 64; a++)
  67.     for (b = 0; b < 64; b++)
  68.       {
  69.     d = abs (column (a) - column (b));
  70.     di = abs (row (a) - row (b));
  71.     taxidata[a][b] = d + di;
  72.     distdata[a][b] = (d > di ? d : di);
  73.       }
  74. #ifdef KILLT
  75.   Initialize_killt ();
  76. #endif
  77. }
  78.  
  79. const short Stboard[64] =
  80. {rook, knight, bishop, queen, king, bishop, knight, rook,
  81.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  82.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  85.  rook, knight, bishop, queen, king, bishop, knight, rook};
  86. const short Stcolor[64] =
  87. {white, white, white, white, white, white, white, white,
  88.  white, white, white, white, white, white, white, white,
  89.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  90.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  91.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  92.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  93.  black, black, black, black, black, black, black, black,
  94.  black, black, black, black, black, black, black, black};
  95. short board[64], color[64];
  96.  
  97. /* given epsquare, from where can a pawn be taken? */
  98. const short epmove1[64] =
  99. {0, 1, 2, 3, 4, 5, 6, 7,
  100.  8, 9, 10, 11, 12, 13, 14, 15,
  101.  16, 24, 25, 26, 27, 28, 29, 30,
  102.  24, 25, 26, 27, 28, 29, 30, 31,
  103.  32, 33, 34, 35, 36, 37, 38, 39,
  104.  40, 32, 33, 34, 35, 36, 37, 38,
  105.  48, 49, 50, 51, 52, 53, 54, 55,
  106.  56, 57, 58, 59, 60, 61, 62, 63};
  107. const short epmove2[64] =
  108. {0, 1, 2, 3, 4, 5, 6, 7,
  109.  8, 9, 10, 11, 12, 13, 14, 15,
  110.  25, 26, 27, 28, 29, 30, 31, 23,
  111.  24, 25, 26, 27, 28, 29, 30, 31,
  112.  32, 33, 34, 35, 36, 37, 38, 39,
  113.  33, 34, 35, 36, 37, 38, 39, 47,
  114.  48, 49, 50, 51, 52, 53, 54, 55,
  115.  56, 57, 58, 59, 60, 61, 62, 63};
  116.  
  117.  
  118. /*
  119.  * nextpos[piece][from-square] , nextdir[piece][from-square] gives vector of
  120.  * positions reachable from from-square in ppos with piece such that the
  121.  * sequence    ppos = nextpos[piece][from-square]; pdir =
  122.  * nextdir[piece][from-square]; u = ppos[sq]; do { u = ppos[u]; if(color[u]
  123.  * != neutral) u = pdir[u]; } while (sq != u); will generate the sequence of
  124.  * all squares reachable from sq.
  125.  *
  126.  * If the path is blocked u = pdir[sq] will generate the continuation of the
  127.  * sequence in other directions.
  128.  */
  129.  
  130. unsigned char __far nextpos[8][64][64];
  131. unsigned char __far nextdir[8][64][64];
  132.  
  133. /*
  134.  * ptype is used to separate white and black pawns, like this; ptyp =
  135.  * ptype[side][piece] piece can be used directly in nextpos/nextdir when
  136.  * generating moves for pieces that are not black pawns.
  137.  */
  138. const short ptype[2][8] =
  139. {
  140.   no_piece, pawn, knight, bishop, rook, queen, king, no_piece,
  141.   no_piece, bpawn, knight, bishop, rook, queen, king, no_piece};
  142.  
  143. /* data used to generate nextpos/nextdir */
  144. static const short direc[8][8] =
  145. {
  146.   0, 0, 0, 0, 0, 0, 0, 0,
  147.   10, 9, 11, 0, 0, 0, 0, 0,
  148.   8, -8, 12, -12, 19, -19, 21, -21,
  149.   9, 11, -9, -11, 0, 0, 0, 0,
  150.   1, 10, -1, -10, 0, 0, 0, 0,
  151.   1, 10, -1, -10, 9, 11, -9, -11,
  152.   1, 10, -1, -10, 9, 11, -9, -11,
  153.   -10, -9, -11, 0, 0, 0, 0, 0};
  154. static const short max_steps[8] =
  155. {0, 2, 1, 7, 7, 7, 1, 2};
  156. static const short nunmap[120] =
  157. {
  158.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  159.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  160.   -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
  161.   -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
  162.   -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
  163.   -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
  164.   -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
  165.   -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
  166.   -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
  167.   -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
  168.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  169.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  170.  
  171. int InitFlag = false;
  172. void
  173. Initialize_moves (void)
  174.  
  175. /*
  176.  * This procedure pre-calculates all moves for every piece from every square.
  177.  * This data is stored in nextpos/nextdir and used later in the move
  178.  * generation routines.
  179.  */
  180.  
  181. {
  182.   short ptyp, po, p0, d, di, s, delta;
  183.   unsigned char *ppos, *pdir;
  184.   short dest[8][8];
  185.   short steps[8];
  186.   short sorted[8];
  187.  
  188.   for (ptyp = 0; ptyp < 8; ptyp++)
  189.     for (po = 0; po < 64; po++)
  190.       for (p0 = 0; p0 < 64; p0++)
  191.     {
  192.       nextpos[ptyp][po][p0] = (unsigned char) po;
  193.       nextdir[ptyp][po][p0] = (unsigned char) po;
  194.     }
  195.   for (ptyp = 1; ptyp < 8; ptyp++)
  196.     for (po = 21; po < 99; po++)
  197.       if (nunmap[po] >= 0)
  198.     {
  199.       ppos = nextpos[ptyp][nunmap[po]];
  200.       pdir = nextdir[ptyp][nunmap[po]];
  201.       /* dest is a function of direction and steps */
  202.       for (d = 0; d < 8; d++)
  203.         {
  204.           dest[d][0] = nunmap[po];
  205.           delta = direc[ptyp][d];
  206.           if (delta != 0)
  207.         {
  208.           p0 = po;
  209.           for (s = 0; s < max_steps[ptyp]; s++)
  210.             {
  211.               p0 = p0 + delta;
  212.  
  213.               /*
  214.                * break if (off board) or (pawns only move two
  215.                * steps from home square)
  216.                */
  217.               if ((nunmap[p0] < 0) || (((ptyp == pawn) || (ptyp == bpawn))
  218.                            && ((s > 0) && ((d > 0) || (Stboard[nunmap[po]] != pawn)))))
  219.             break;
  220.               else
  221.             dest[d][s] = nunmap[p0];
  222.             }
  223.         }
  224.           else
  225.         s = 0;
  226.  
  227.           /*
  228.            * sort dest in number of steps order currently no sort
  229.            * is done due to compability with the move generation
  230.            * order in old gnu chess
  231.            */
  232.           steps[d] = s;
  233.           for (di = d; s > 0 && di > 0; di--)
  234.         if (steps[sorted[di - 1]] == 0)    /* should be: < s */
  235.           sorted[di] = sorted[di - 1];
  236.         else
  237.           break;
  238.           sorted[di] = d;
  239.         }
  240.  
  241.       /*
  242.        * update nextpos/nextdir, pawns have two threads (capture
  243.        * and no capture)
  244.        */
  245.       p0 = nunmap[po];
  246.       if (ptyp == pawn || ptyp == bpawn)
  247.         {
  248.           for (s = 0; s < steps[0]; s++)
  249.         {
  250.           ppos[p0] = (unsigned char) dest[0][s];
  251.           p0 = dest[0][s];
  252.         }
  253.           p0 = nunmap[po];
  254.           for (d = 1; d < 3; d++)
  255.         {
  256.           pdir[p0] = (unsigned char) dest[d][0];
  257.           p0 = dest[d][0];
  258.         }
  259.         }
  260.       else
  261.         {
  262.           pdir[p0] = (unsigned char) dest[sorted[0]][0];
  263.           for (d = 0; d < 8; d++)
  264.         for (s = 0; s < steps[sorted[d]]; s++)
  265.           {
  266.             ppos[p0] = (unsigned char) dest[sorted[d]][s];
  267.             p0 = dest[sorted[d]][s];
  268.             if (d < 7)
  269.               pdir[p0] = (unsigned char) dest[sorted[d + 1]][0];
  270.  
  271.             /*
  272.              * else is already initialized
  273.              */
  274.           }
  275.         }
  276.     }
  277. }
  278.  
  279. void
  280. NewGame (void)
  281.  
  282. /*
  283.  * Reset the board and other variables to start a new game.
  284.  */
  285.  
  286. {
  287.   short l, c, p;
  288.  
  289.   stage = stage2 = -1;        /* the game is not yet started */
  290.   flag.illegal = flag.mate = flag.quit = flag.bothsides = false;
  291.   flag.onemove = flag.force = false;
  292.   flag.hash = flag.easy = flag.beep = flag.rcptr = true;
  293. #if defined(MSDOS) && !defined(SEVENBIT)
  294.   flag.rv = false;
  295. #else
  296.   if (!InitFlag) {    /* use user settings o.w. */
  297.     flag.post = false;
  298.     flag.reverse = false;
  299.     flag.stars = false;
  300.     flag.rv = true;
  301.     flag.material = true;
  302.     flag.shade = false;
  303.  
  304. #ifdef COORDSOFF
  305.     flag.coords = 0;
  306. #else
  307.     flag.coords = true;
  308. #endif
  309.  
  310. #ifdef RANDOMIZE
  311.     dither = DITHER;
  312. #else
  313.     dither = 0;
  314. #endif
  315.   }
  316.   flag.musttimeout = false;
  317.  
  318. #endif /* MSDOS && !SEVENBIT */
  319.  
  320.   NodeCnt = et0 = epsquare = 0;
  321.   XCmore = 0;
  322.   WAwindow = WAWNDW;
  323.   WBwindow = WBWNDW;
  324.   BAwindow = BAWNDW;
  325.   BBwindow = BBWNDW;
  326.   xwndw = BXWNDW;
  327.   if (!MaxSearchDepth)
  328.     MaxSearchDepth = MAXDEPTH - 1;
  329.   contempt = 0;
  330.   GameCnt = 0;
  331.   Game50 = 1;
  332.   hint = 0x0C14;
  333.   ZeroRPT ();
  334.   Developed[white] = Developed[black] = false;
  335.   castld[white] = castld[black] = false;
  336.   PawnThreat[0] = CptrFlag[0] = false;
  337.   Pscore[0] = 12000;
  338.   Tscore[0] = 12000;
  339.   opponent = white;
  340.   computer = black;
  341.   for (l = 0; l < TREE; l++)
  342.     Tree[l].f = Tree[l].t = 0;
  343. #if ttblsz
  344.   ZeroTTable ();
  345. #endif /* ttblsz */
  346.   srand ((unsigned int) 1);
  347.   if (!InitFlag)
  348.     {
  349.       for (c = white; c <= black; c++)
  350.     for (p = pawn; p <= king; p++)
  351.       for (l = 0; l < 64; l++)
  352.         {
  353.           hashcode[c][p][l].key = (((unsigned long) urand ()));
  354.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 16);
  355.           hashcode[c][p][l].bd = (((unsigned long) urand ()));
  356.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 16);
  357. #ifdef LONG64
  358.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 32);
  359.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 48);
  360.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 32);
  361.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 48);
  362. #endif
  363.         }
  364.     }
  365.   for (l = 0; l < 64; l++)
  366.     {
  367.       board[l] = Stboard[l];
  368.       color[l] = Stcolor[l];
  369.       Mvboard[l] = 0;
  370.     }
  371.   ClrScreen ();
  372.   InitializeStats ();
  373.   time0 = time ((long *) 0);
  374.   ElapsedTime (1);
  375.   flag.regularstart = true;
  376.   Book = BOOKFAIL;
  377.   if (!InitFlag)
  378.     {
  379.       if (TCflag)
  380.     SetTimeControl ();
  381.       else if (MaxResponseTime == 0)
  382.     SelectLevel ();
  383.       UpdateDisplay (0, 0, 1, 0);
  384.       ShowMessage("Reading opening book...");
  385.       GetOpenings ();
  386.       InitFlag = true;
  387.     }
  388.   else ResetTimeControl();
  389.  
  390.   hashbd = hashkey = 0;
  391. }
  392.  
  393. void
  394. InitConst (char *lang)
  395. {
  396.   FILE *constfile;
  397.   char s[256];
  398.   char sl[5];
  399.   int len, entry;
  400.   char *r, *p, *q;
  401. #ifdef AMIGA
  402.   char langname[256];
  403.   strcpy(langname, libdir);
  404.   AddPart(langname, FilePart(LANGFILE), 256);
  405.   constfile = fopen (langname, "r");
  406. #else
  407.   constfile = fopen (LANGFILE, "r");
  408. #endif
  409.   if (!constfile)
  410.     {
  411.       fprintf (stderr, "NO LANGFILE\n");
  412.       exit (1);
  413.     }
  414.   while (fgets (s, sizeof (s), constfile))
  415.     {
  416.       if (s[0] == '!')
  417.     continue;
  418.       len = strlen (s);
  419.       for (q = &s[len]; q > &s[8]; q--)
  420.     if (*q == '}')
  421.       break;
  422.       if (q == &s[8])
  423.     {
  424.       fprintf (stderr, "{ error in cinstfile\n");
  425.       exit (1);
  426.     }
  427.       *q = '\0';
  428.       if (s[3] != ':' || s[7] != ':' || s[8] != '{')
  429.     {
  430.       fprintf (stderr, "Langfile format error %s\n", s);
  431.       exit (1);
  432.     }
  433.       s[3] = s[7] = '\0';
  434.       if (lang == NULL)
  435.     {
  436.       lang = sl;
  437.       strcpy (sl, &s[4]);
  438.     }
  439.       if (strcmp (&s[4], lang))
  440.     continue;
  441.       entry = atoi (s);
  442.       if (entry < 0 || entry >= CPSIZE)
  443.     {
  444.       fprintf (stderr, "Langfile number error\n");
  445.       exit (1);
  446.     }
  447.       for (q = p = &s[9]; *p; p++)
  448.     {
  449.       if (*p != '\\')
  450.         {
  451.           *q++ = *p;
  452.         }
  453.       else if (*(p + 1) == 'n')
  454.         {
  455.           *q++ = '\n';
  456.           p++;
  457.         }
  458.     }
  459.       *q = '\0';
  460.       if (entry < 0 || entry > 255)
  461.     {
  462.       fprintf (stderr, "Langfile error %d\n", entry);
  463.       exit (0);
  464.     }
  465.       CP[entry] = (char *) malloc ((unsigned) strlen (&s[9]) + 1);
  466.       if (CP[entry] == NULL)
  467.     {
  468.       perror ("malloc");
  469.       exit (0);
  470.     }
  471.       strcpy (CP[entry], &s[9]);
  472.  
  473.     }
  474.   fclose (constfile);
  475. }
  476.  
  477. #ifdef ttblsz
  478. void
  479. Initialize_ttable ()
  480. {
  481.   if (rehash < 0)
  482.     rehash = MAXrehash - 1;
  483. }
  484.  
  485. #endif /* ttblsz */
  486.